package com.beerq.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.AppConfig;
import com.beerq.util.BasicHttp;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;
import com.beerq.util.Utility;
import com.beerq.view.friends.FriendsManager;
import com.beerq.view_manager.CommonManager;

import java.io.File;
import java.util.Calendar;

import static com.beerq.util.Constants.userInfo;

public class ModifyHeadImageActivity extends BaseActivity {

    String path = "";
    String base64String="";
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_head_image);

        mContext=this;

        Utility.getPictureOnActivity(this);

    }


    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                finish();
            }else if(msg.what==1){

                CommonManager.modifyImage(mContext,Constants.userInfo.getWxNo()+ System.currentTimeMillis()+path.substring(path.lastIndexOf("."),path.length()) , base64String, Constants.userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        FriendsManager.getMyFavouriteAndLikes(mContext, userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
                            @Override
                            public void onResponse(String resonseData) {
                                JSONObject temp= JSONObject.parseObject(resonseData).getJSONArray("Result").getJSONObject(0);


                                userInfo.setHeadPic(temp.getString("WxHeadImg"));

                                String str=JSONObject.toJSONString(userInfo);
                                BeerQPref.getInstance(mContext).setUserInfo(str);

                                finish();



                            }

                            @Override
                            public void onFailure(String resonseData) {

                            }
                        });
                    }

                    @Override
                    public void onFailure(String resonseData) {

                    }
                });
            }
            return false;
        }
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("onActivityResult", "request code " + requestCode + " result Code " + resultCode + " Data " + data);
        try {
            if (resultCode == RESULT_OK) {


                Bitmap b = null;
                if (requestCode == Constants.GALLERY_APP) {
                    path = Utility.getRealPathFromURI(this, data.getData());
//                    b = Utility.decodeFile(new File(path));
                    //updateSavedData();

                } else if (requestCode == Constants.CAMERA_APP) {

                    path = Utility.getRealPathFromURI(this, Constants.mCamRequestedUri);
                    Log.e("Step 1", path + "");
//                    b = Utility.decodeFile(new File(path));
//                    isFromImageTaken = true;


                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        base64String= AppConfig.compressImageReturnBase64(path);

                        Message message= Message.obtain();
                        message.what=1;
                        mHandler.sendMessage(message);



//                            base64String=  CommonManager.bitmap2StrByBase64(bitmap,mAddImageManager.getImages().get(0).path);
//

                    }
                }).start();

                Log.e("Step 6", "Bitmap" + b);
//                mClickedView.setImageBitmap(b);
//                mClickedView.setTag(path);
//                saveData();
//                resetUploadedValue();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }


    }
}
